package org.strangewt.widgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Text;

public class CompositeBase extends Composite {

	public CompositeBase(Composite parent, int style) {
		super(parent, style);
	}

	// container ////////////////////////////////
	
	/**
	 * <code>Composite</code> with a <code>GridLayout</code> and one row.
	 * @param parent
	 * @return
	 */
	public Composite createComposite(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		
		composite.setLayoutData(createGridDataForInputFields());
		
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		
		return composite;
	}
	
	/**
	 * <code>Composite</code> with a <code>GridLayout</code> and n rows.
	 * @param parent
	 * @param numColumns
	 * @return
	 */
	public Composite createComposite(Composite parent, int numColumns) {
		Composite composite = new Composite(parent, SWT.NONE);
		
		composite.setLayoutData(createGridDataForInputFields());
		
		GridLayout layout = new GridLayout(numColumns, false);
		composite.setLayout(layout);
		
		return composite;
	}
	
	/**
	 * <code>Composite</code> with an assigned <code>Layout</code>.
	 * @param parent
	 * @param layout
	 * @return
	 */
	public Composite createComposite(Composite parent, Layout layout) {
		Composite composite = new Composite(parent, SWT.NONE);
		
		composite.setLayoutData(createGridDataForInputFields());
		
		composite.setLayout(layout);
		return composite;
	}
	
	/**
	 * <code>Composite</code> for holding a horizontal row of buttons.<br>
	 * - The composite fills horizontally.<br>
	 * - The <code>GridLayout</code> has no margins.  
	 * @param parent
	 * @param numColumns
	 * @return
	 */
	public Composite createCompositeForButtons(Composite parent, int numColumns) {
		Composite composite = new Composite(parent, SWT.NONE);
		
		composite.setLayoutData(createGridDataForInputFields());
		
		composite.setLayout(createGridLayoutNoMargins(numColumns));
		
		return composite;
	}
	
	/**
	 *  <code>Group</code> with a <code>GridLayout</code> and one row.
	 * @param parent
	 * @param text
	 * @return
	 */
	public Group createGroup(Composite parent, String text) {
		Group group = new Group(parent, SWT.NONE);
		group.setText(text);
		
		group.setLayoutData(createGridDataForInputFields());
		
		GridLayout layout = new GridLayout();
		group.setLayout(layout);
		
		return group;
	}
	
	/**
	 * <code>Composite</code> with a <code>GridLayout</code> and n rows.
	 * @param parent
	 * @param text
	 * @param numColumns
	 * @return
	 */
	public Group createGroup(Composite parent, String text, int numColumns) {
		Group group = new Group(parent, SWT.NONE);
		group.setText(text);
		
		group.setLayoutData(createGridDataForInputFields());
		
		GridLayout layout = new GridLayout(numColumns, false);
		group.setLayout(layout);
		
		return group;
	}
	
	// widgets ////////////////////////////////////////
	
	/**
	 * <code>Label</code> with text.
	 * @param parent
	 * @param text
	 * @return
	 */
	public Label createLabel(Composite parent, String text) {
		Label label = new Label(parent, SWT.NONE);
		label.setText(text);
		return label;
	}
	
	/**
	 * <code>Label</code> with text for multi column input fields.<br>
	 * - verticalAlignment = SWT.TOP
	 * @param parent
	 * @param text
	 * @return
	 */
	public Label createLabelTop(Composite parent, String text) {
		Label label = new Label(parent, SWT.NONE);
		label.setText(text);
		label.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
		return label;
	}
	
	/**
	 * <code>Label</code> with text.
	 * @param parent
	 * @param text
	 * @return
	 */
	public Label createLabel(Composite parent, String text, int horizontalSpan, int verticalSpan) {
		Label label = new Label(parent, SWT.NONE);
		label.setText(text);
		
		GridData gd = new GridData();
		gd.horizontalSpan = horizontalSpan;
		gd.verticalSpan = verticalSpan;
		label.setLayoutData(gd);
		
		return label;
	}
	
	/**
	 * <code>Text</code> with border that fills up a line.<br>
	 * - horizontalAlignment = SWT.FILL<br>
	 * - grabExcessHorizontalSpace = true
	 * @param parent
	 * @return
	 */
	public Text createText(Composite parent) {
		Text text = new Text(parent, SWT.BORDER);
		text.setLayoutData(createGridDataForInputFields());
		return text;
	}
	
	/**
	 * <code>Text</code> with border that fills up a line.<br>
	 * - horizontalAlignment = SWT.FILL<br>
	 * - grabExcessHorizontalSpace = true
	 * @param parent
	 * @return
	 */
	public Text createText(Composite parent, String string) {
		Text text = new Text(parent, SWT.BORDER);
		text.setText(string);
		text.setLayoutData(createGridDataForInputFields());
		return text;
	}
	
	/**
	 * Multiline Textfield with border with vertical scroll bars.
	 * @param parent
	 * @param widthHint
	 * @param heightHint
	 * @return
	 */
	public Text createTextField(Composite parent, int widthHint, int heightHint) {
		Text text = new Text(parent, SWT.V_SCROLL | SWT.MULTI | SWT.WRAP | SWT.BORDER);
		GridData gd = createGridDataForInputFields();
		if(widthHint != 0) {
			gd.widthHint = widthHint;
		}
		if(heightHint != 0) {
			gd.heightHint = heightHint;
		}
		text.setLayoutData(gd);
		return text;
	}
	
	/**
	 * Multiline Textfield with border and vertical scroll bars. Additional you 
	 * can specify the verticalSpan. This allows you to have more 
	 * than one <code>Label</code> in front of the Textfield.
	 * @param parent
	 * @param widthHint
	 * @param heightHint
	 * @param horizontalSpan
	 * @param verticalSpan
	 * @return
	 */
	public Text createTextField(Composite parent, int widthHint, int heightHint, int horizontalSpan, int verticalSpan) {
		Text text = new Text(parent, SWT.V_SCROLL | SWT.MULTI | SWT.WRAP | SWT.BORDER);
		GridData gd = createGridDataForInputFields();
		gd.widthHint = widthHint;
		gd.heightHint = heightHint;
		gd.horizontalSpan = horizontalSpan;
		gd.verticalSpan = verticalSpan;
		text.setLayoutData(gd);
		return text;
	}
	
	/**
	 * Textfield with a Label in front of.
	 *
	 * @param parent
	 * @param labelText
	 * @return
	 */
	public Text createTextWithLabel(Composite parent, String labelText) {
		createLabel(parent, labelText);
		return createText(parent);
	}
	
	/**
	 * Pushing <code>Button</code> with a name.
	 * @param parent
	 * @param name
	 * @return
	 */
	public Button createButton(Composite parent, String name) {
		return createButton(parent, name, null);
	}
	
	/**
	 * Pushing <code>Button</code> with a name and <code>SelectionListener</code>.
	 * @param parent
	 * @param name
	 * @param listener
	 * @return
	 */
	public Button createButton(Composite parent, String name, SelectionListener listener) {
		Button button = new Button(parent, SWT.PUSH);
		button.setText(name);
		if(listener != null) {
			button.addSelectionListener(listener);
		}
		return button;
	}
	
	/**
	 * <code>Button</code> with a name, style and <code>SelectionListener</code>.
	 * @param parent
	 * @param style
	 * @param name
	 * @param listener
	 * @return
	 */
	public Button createButton(Composite parent, int style, String name, SelectionListener listener) {
		Button button = new Button(parent, style);
		button.setText(name);
		if(listener != null) {
			button.addSelectionListener(listener);
		}
		return button;
	}
	
	// layouts ///////////////////////////////////////
	
	/**
	 * new GridData(SWT.FILL, SWT.FILL, true, true);
	 * @return
	 */
	public GridData createGridDataFillFill() {
		GridData gd =  new GridData(SWT.FILL, SWT.FILL, true, true);
		return gd;
	}
	
	/**
	 * new GridData(SWT.FILL, SWT.CENTER, true, false); <br>
	 * gd.widthHint = 50;
	 * @return
	 */
	public GridData createGridDataForInputFields() {
		GridData gd =  new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd.widthHint = 50;
		return gd;
	}
	
	/**
	 * new GridData(SWT.FILL, SWT.CENTER, true, false); <br>
	 * gd.horizontalSpan = horizontalSpan;
	 * gd.widthHint = 50;
	 * @return
	 */
	public GridData createGridDataForInputFields(int horizontalSpan) {
		GridData gd =  new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd.horizontalSpan = horizontalSpan;
		gd.widthHint = 50;
		return gd;
	}
	
	/**
	 * new GridData(SWT.FILL, SWT.CENTER, true, false); <br>
	 * gd.widthHint = widthHint;
	 * gd.heightHint = heightHint;
	 */
	public GridData createGridDataForInputFields(int widthHint, int heightHint) {
		GridData gd =  new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd.widthHint = widthHint;
		gd.heightHint = heightHint;
		return gd;
	}
	
	/**
	 * new GridData(SWT.FILL, SWT.CENTER, true, false); <br>
	 * gd.widthHint = widthHint;
	 * gd.heightHint = heightHint;
	 * gd.horizontalSpan = horizontalSpan;
	 * gd.verticalSpan = verticalSpan;
	 */
	public GridData createGridDataForInputFields(int widthHint, int heightHint, int horizontalSpan, int verticalSpan) {
		GridData gd =  new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd.widthHint = widthHint;
		gd.heightHint = heightHint;
		gd.horizontalSpan = horizontalSpan;
		gd.verticalSpan = verticalSpan;
		return gd;
	}
	
	/**
	 * new GridLayout(numColumns, false); <br>
	 * layout.marginHeight = 0;
     * layout.marginWidth = 0;
	 */
	public GridLayout createGridLayoutNoMargins(int numColumns) {
		GridLayout layout = new GridLayout(numColumns, false);
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        return layout;
	}

}
